package com.yangzc.lion.sys.util;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;

import com.yangzc.lion.common.tag.Page;

/**
 * JSON 日期格式处理（java转化为JSON）
 */

public class JsonDateValueProcessor implements JsonValueProcessor {

	private String datePattern = DateUtil.PATTERN_DATETIME;

	public JsonDateValueProcessor() {
	}

	public JsonDateValueProcessor(String datePattern) {
		this.datePattern = datePattern;
	}

	public Object processArrayValue(Object value, JsonConfig jsonConfig) {
		return process(value);
	}

	private Object process(Object value) {
		if ((value instanceof java.sql.Date)) {
			value = new java.util.Date(((java.sql.Date) value).getTime());
		}
		if (value instanceof java.util.Date) {
			String str = DateUtil
					.convertDateToString((Date) value, datePattern);
			return str;
		}
		return value.toString();
	}

	public Object processObjectValue(String key, Object value,
			JsonConfig jsonConfig) {
		return process(value);
	}

	public static void main(String[] args) {
		Page page = new Page();
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("docCreateTime", new Date());
		list.add(map);
		page.setRows(list);
		System.out.println(StringUtil.pageToString(page));

	}

}
